package com.zretc.points.service;

import com.zretc.api.pojo.MessageEvent;
import com.zretc.common.constant.PointsDic;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RocketMQMessageListener(topic = "addPointsTopic", consumerGroup = "points-consumer-group")
public class pointsCousumer implements RocketMQListener<MessageEvent> {
    @Autowired
    private PointsService pointsService;
    @Override
    @Transactional
    public void onMessage(MessageEvent messageEvent) {
        if ("addRecommendPoints".equals(messageEvent.getAction())) {
            pointsService.addChangeMes(messageEvent.getUserId(), pointsService.getPoints(PointsDic.RECOMMEND),"发表内推");
            pointsService.addPoints(messageEvent.getUserId(), pointsService.getPoints(PointsDic.RECOMMEND));
            System.out.println("消费消息已发送");
        }
        else if("addActivityPoints".equals(messageEvent.getAction())){
            pointsService.addChangeMes(messageEvent.getUserId(), pointsService.getPoints(PointsDic.LOGIN),"发布活动");
            pointsService.addPoints(messageEvent.getUserId(), pointsService.getPoints(PointsDic.ACTIVITY));
        }
        else if("login".equals(messageEvent.getAction())){
            pointsService.addChangeMes(messageEvent.getUserId(), pointsService.getPoints(PointsDic.LOGIN),"登录");
            pointsService.addPoints(messageEvent.getUserId(), pointsService.getPoints(PointsDic.LOGIN));
        }
        else if("addBlogPoints".equals(messageEvent.getAction())){
            pointsService.addChangeMes(messageEvent.getUserId(), pointsService.getPoints(PointsDic.LOGIN),"发表博文");
            pointsService.addPoints(messageEvent.getUserId(), pointsService.getPoints(PointsDic.BLOG));
        }

    }
}
